package edu.vt.sdm.tcga.View;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import edu.vt.sdm.tcga.data.ExpressionLevel2Model;


public class HeatMap extends ViewPart {
	private Canvas canvas = null;
	private SimpleListener sl = null;
	private ExpressionLevel2Model e2m = null;
	
	class SimpleListener implements Listener {
		String name;
		public Boolean bSelectionMode = false;
		public int iStartX = 0;
		public int iStartY = 0;
		public int iEndX = 0, iEndY = 0;
		
		
		public SimpleListener(String name) {
			this.name = name;
		}

		@Override
		public void handleEvent(Event event) {
			// TODO Auto-generated method stub
			switch (event.type) {
			case SWT.MouseDoubleClick:
				bSelectionMode = !bSelectionMode;
				
				if(bSelectionMode == true) {
					iStartX = event.x;
					iStartY = event.y;
				}
				break;
				
			case SWT.MouseMove:
				if(bSelectionMode) {
					iEndX = event.x;
					iEndY = event.y;
	
					Display.getDefault().asyncExec(new Runnable() {
						@Override
						public void run() {
							canvas.redraw();
						}
					});
				}
				break;

			}
//			Shell IShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
//					.getShell();
//
//			MessageDialog.openInformation(IShell, "Information", "X=" + event.x
//					+ ", Y=" + event.y);
		}
	}

	public HeatMap() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);

		sl = new SimpleListener("test");
		canvas.addListener(SWT.MouseDoubleClick, sl);
		canvas.addListener(SWT.MouseMove, sl);


		e2m = ExpressionLevel2Model.getInstance();
//		e2m.setTestData();
		canvas.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				// Color myColor = null;

				GC gc = e.gc;

				int cellwidth = 15, cellheight = 5;

				for (int i = 0; i < e2m.getGeneOfInterestList().size(); i++) {
					for (int j = 0; j < e2m.getSampleOfInterestList().size(); j++) {
						int color = (int) ((e2m.getGeneDataOfInterest()[i][j] - e2m.getMinSignal()) * 255 / (e2m.getMaxSignal() - e2m.getMinSignal()));
						gc.setBackground(new Color(Display.getCurrent(), Math
								.min(color, 255), Math.min(color, 255), 0));
						gc.fillRectangle(0 + j * cellwidth, 0 + i * cellheight,
								cellwidth, cellheight);
					}
				}

				gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
				if(sl.bSelectionMode){
						gc.drawRectangle(Math.min(sl.iStartX, sl.iEndX), 
								Math.min(sl.iEndY, sl.iStartY), 
								Math.abs(sl.iStartX - sl.iEndX),
								Math.abs(sl.iEndY - sl.iStartY));
					}				
			}

		});

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
